package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Teacher;
import com.itheima.edu.info.manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    private Scanner sc = new Scanner(System.in);
    private TeacherService teacherService = new TeacherService();
    private String id;

    public void start() {
        TeacherLoop:
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            int A = sc.nextInt();
            switch (A) {
                case 1:
                    // System.out.println("添加老师");
                    addTeacher();
                    break;
                case 2:
                    // System.out.println("删除老师");
                    removeTeacher();
                    break;
                case 3:
                    // System.out.println("修改老师");
                    changeStudent();
                    break;
                case 4:
                    // System.out.println("查看老师");
                    findAllTeacher();
                    break;
                case 5:
                    System.out.println("退出");
                    break TeacherLoop;
                default:
                    System.out.println("请输入正确选项");
                    break;
            }
        }
    }

    private void changeStudent() {
        System.out.println("请输入要修改的老师工号");
        id = sc.next();
        boolean flog = teacherService.changeTeacher(id);
        if (flog) {
            System.out.println("success");
        }else
            System.out.println("default");

    }

    private void removeTeacher() {
        id = sc.next();
        boolean flog = teacherService.removeTeacher(id);
        if (flog) {
            System.out.println("success");
        }else
            System.out.println("default");

    }

    private void findAllTeacher() {
        Teacher[] ArrayTeacher = teacherService.findAllTeacher();
        if (ArrayTeacher == null) {
            System.out.println("信息不存在");
            return;
        }
        System.out.println("工号\t\t姓名\t年龄\t生日");
        for (Teacher teacher : ArrayTeacher) {
            if (teacher != null) {
                System.out.println(teacher.getId() + teacher.getName() + teacher.getAge() + teacher.getBirthday());
            }
        }
    }

    private void addTeacher() {
        while (true) {
            System.out.println("请输入添加的id");
            id = sc.next();
            boolean flog = teacherService.isExists(id);
            if (flog) {
                System.out.println("学号重复");
            } else break;
        }
        System.out.println("请输入添加的姓名");
        String name = sc.next();
        System.out.println("请输入添加的年龄");
        String age = sc.next();
        System.out.println("请输入添加的生日");
        String birthday = sc.next();
        Teacher stn = new Teacher();
        stn.setId(id);
        stn.setName(name);
        stn.setAge(age);
        stn.setBirthday(birthday);
        boolean flog = teacherService.addTeacher(stn);
        if (flog) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }
}

